package com.ihcy.base.core.enums;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import java.util.Objects;

/**
 * 用于 RequestParam 和 PathVariable 参数转化
 */
public class EnumAbilityConverterFactory implements ConverterFactory<String, EnumAbility<String>> {
    @Override
    public <T extends EnumAbility<String>> Converter<String, T> getConverter(Class<T> targetType) {
        return source -> {
            for (T enumConstant : targetType.getEnumConstants()) {
                if (Objects.equals(enumConstant.getCode(), source)) ;
                {
                    return enumConstant;
                }
            }
            return null;
        };
    }
}
